#!/bin/bash

DIR= # DIR=/data1/ehserver/server/user
VERSION= #VERSION=0.8.0
HOST= # HOST=10.1.10.79

while getopts "H:v:d:h" opt; do
    case "$opt" in
    d) DIR=$OPTARG
	;;
    H) HOST=$OPTARG
	;;
    v) VERSION=$OPTARG
    ;;
    h)
    	printf "Usage:\n\tdeploy.sh -H <server host> -v <version> -d <deployed dir>\n"
        exit 0
        ;;
    esac
done

echo "deploying ...0"
ssh root@${HOST} "[ -d ${DIR} ] || mkdir -p ${DIR}"

echo "deploying ...1"
#exist=$(ssh root@10.1.120.105 "[ -f /data1/ehserver/server/user/config.yml ] && echo yes")
scp ../engine/target/ehuser-${VERSION}-SNAPSHOT.war root@${HOST}:${DIR}
#scp ../engine/target/classes/config.yml root@${HOST}:${DIR}
#scp ../engine/target/classes/logback.xml root@${HOST}:${DIR}

echo "deploying ...2"
scp server.sh root@${HOST}:${DIR}

echo "deploying ...3"
ssh root@${HOST} "source /etc/profile && cd ${DIR} && chmod +x ./server.sh && ./server.sh restart ${VERSION}"
echo "deploy success"